package thread.threaddemo;

import java.util.concurrent.TimeUnit;

/**
 * @Author: wenjingyuan
 * @Date: 2023/01/14/19:04
 * @Description:解决死锁，破坏请求并持有条件
 */
public class UnDeadLock {
    public static void main(String[] args) {
        Object lockA=new Object();
        Object lockB=new Object();
        Thread t1=new Thread(()->{
            synchronized (lockA){
                System.out.println("线程1拿到了锁A");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                synchronized (lockB){
//                    System.out.println("线程1拿到了锁B");
//                    //业务代码
//                    System.out.println("线程1释放了锁B");
//                }
                System.out.println("线程1释放了锁A");
            }
        },"线程1");
        t1.start();

        Thread t2=new Thread(()->{
            synchronized (lockB){
                System.out.println("线程2拿到了锁B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                synchronized (lockA){
//                    System.out.println("线程2拿到了锁A");
//                    //业务代码
//                    System.out.println("线程2释放了锁A");
//                }
                System.out.println("线程2释放了锁B");
            }
        },"线程2");
        t2.start();
    }
}
